﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hp.Vertica.Client.Internal;
using System.Reflection;

namespace Hp.Vertica.Migration.SqlServer
{
    public abstract class SqlDataType
    {
        public abstract String Name { get; }
        public virtual Int32 Size { get; set; }
        public virtual Int32 Precision { get; set; }
        public virtual Int32 Scale { get; set; }

        public override string ToString()
        {
            return this.Name;
        }

        public abstract VerticaDataType VerticaDataType { get; }
        public abstract System.Data.SqlDbType SystemDataType { get; }

        private static Dictionary<String, String> sqlAliasTable = new Dictionary<string, string>();
        private static Dictionary<String, Type> sqlTypeTable = new Dictionary<string, Type>();
        public static SqlDataType Create(String tName, Int32 maxLength, Int32 precision, Int32 scale)
        {
            tName = sqlAliasTable.ContainsKey(tName.ToUpper()) ? sqlAliasTable[tName.ToUpper()] : tName.ToUpper();
            if (!sqlTypeTable.ContainsKey(tName))
            {
                foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
                {
                    if (("SqlDataType_" + tName).ToUpper() == t.Name.ToUpper())
                    {
                        sqlTypeTable.Add(tName, t);
                        break;
                    }
                }
            }
            Type type = sqlTypeTable[tName];
            SqlDataType result = type.GetConstructor(new Type[] { }).Invoke(new Object[] { }) as SqlDataType;
            result.Size = maxLength;
            result.Precision = precision;
            result.Scale = scale;
            return result;
        }
    }
}
